package sunwin.yoms.service.resource;

import com.github.pagehelper.PageHelper;
import jodd.datetime.JDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.comm.model.TimeSpan;
import sunwin.yoms.dao.StadiumDao;
import sunwin.yoms.domain.resource.StadiumDomain;
import sunwin.yoms.domain.resource.StadiumStateDomain;

import java.sql.Time;
import java.util.List;

/**
 * Created by watson on 2015/7/30.
 */
@Service
public class StadiumService {
    @Autowired
    private StadiumDao stadiumDao;

    /**
     * 更新热度
     */
    public void updateHot(int sid) throws YomsException {
        StadiumDomain domain = stadiumDao.getDomainById(sid);
        domain.setStadiumHot(domain.getStadiumHot() + 1);

        int result = stadiumDao.updateDomain(domain);
        if (result != 1) {
            throw YomsException.getInstance("更新场馆热度失败");
        }
    }

    /**
     * 按热度排名
     *
     * @throws YomsException
     */
    public List<StadiumDomain> findStadiumHot(int pageSize) {
        PageHelper.startPage(1, pageSize);
        PageHelper.orderBy("stadium_hot desc");
        return stadiumDao.getDomainCollectionExtend(null, null, null);
    }

    /**
     * 查找场馆
     *
     * @param stadiumDomain
     * @param yomsPage
     * @param startTime
     * @param endTime
     * @return
     * @throws YomsException
     */
    public List<StadiumDomain> findStadium(StadiumDomain stadiumDomain, YomsPage yomsPage, Time startTime, Time endTime) throws YomsException {
        com.github.pagehelper.Page total = PageHelper.startPage(yomsPage.getPageIndex(), yomsPage.getPageSize(), true);
        List<StadiumDomain> stadiumDomains = stadiumDao.getDomainCollectionExtend(stadiumDomain, startTime, endTime);
        yomsPage.setPageCount(total.getTotal() % yomsPage.getPageSize() == 0 ? total.getTotal() / yomsPage.getPageSize() : total.getTotal() / yomsPage.getPageSize() + 1);
        if (stadiumDomains.size() == 0) {
            throw new YomsException("没有找到数据", 1);
        }
        return stadiumDomains;
    }

    /**
     * 查询场馆
     */
    public List<StadiumDomain> getStadiumDomain(StadiumDomain stadiumDomain){
        List<StadiumDomain> domains = stadiumDao.getDomainCollection(stadiumDomain);
        return domains;
    }

    /**
     * 获取场馆状态
     * @param sid
     * @return
     */
    public StadiumStateDomain getStadiumState(int sid) {
        try {
            StadiumDomain stadiumDomain = this.getStadiumById(sid);
            return stadiumDomain.getStadiumStateDomain();
        } catch (YomsException e) {
            return null;
        }
    }

    /**
     * 根据场馆ID获取场馆营业时间段
     *
     * @param sid
     * @return TimeSpan
     * @throws YomsException
     */
    public TimeSpan getStadiumBusinessTimeSpan(int sid) throws YomsException {
        StadiumDomain stadiumDomain = this.getStadiumById(sid);
        JDateTime stratTime = new JDateTime(stadiumDomain.getStadiumStarttime());
        JDateTime endTime = new JDateTime(stadiumDomain.getStadiumEndtime());

        TimeSpan timeSpan = new TimeSpan();
        timeSpan.setStartTime(stratTime.convertToSqlTime());
        timeSpan.setEndTime(endTime.convertToSqlTime());

        return timeSpan;
    }

    /**
     * 根据类型获取场馆
     *
     * @param gid
     * @return
     */
    public List<StadiumDomain> getStadiumByGroup(int gid) {
        StadiumDomain domain = new StadiumDomain();
        domain.setStadiumGroupId(gid);

        List<StadiumDomain> domains = stadiumDao.getDomainCollection(domain);

        return domains;
    }

    /**
     * 更新指定场馆的热度
     */
    public void updateStadium(int sid) {
        StadiumDomain domain = stadiumDao.getDomainById(sid);
        if (domain != null) {
            long hot = domain.getStadiumHot();
            if (hot < Long.MAX_VALUE) {

                StadiumDomain stadium = new StadiumDomain();
                stadium.setStadiumId(domain.getStadiumId());
                stadium.setStadiumHot(hot + 1);

                stadiumDao.updateDomain(stadium);
            }
        }
    }

    /**
     * 根据ID查找场馆
     *
     * @param stadiumId
     * @return
     */
    public StadiumDomain getStadiumById(int stadiumId) throws YomsException {
        StadiumDomain stadiumDomain = stadiumDao.getDomainById(stadiumId);
        if (stadiumDomain == null) {
            throw new YomsException("无法查找到指定场馆");
        } else {
            return stadiumDomain;
        }
    }

    /**
     * 获取场馆总数量
     *
     * @return
     */
    public int getStadiumCount() {
        return stadiumDao.getDomainCount();
    }

    /**
     * 创建场馆
     *
     * @param stadiumDomain
     * @throws YomsException
     */
    public void createSatdium(StadiumDomain stadiumDomain) throws YomsException {
        StadiumDomain stadium = new StadiumDomain();
        stadium.setStadiumName(stadiumDomain.getStadiumName());
        List<StadiumDomain> stadiumDomains = stadiumDao.getDomainByCondition(stadium);
        if (stadiumDomains.size() > 0) {
            throw new YomsException("该场馆已存在");
        }
        if (stadiumDao.addDomain(stadiumDomain) != 1) {
            throw new YomsException("增加公园场馆是失败");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @throws YomsException
     */
    public void deleteSatdium(int id) throws YomsException {
        StadiumDomain stadiumDomain = new StadiumDomain();
        stadiumDomain.setStadiumId(id);
        StadiumDomain domain = stadiumDao.getDomainById(stadiumDomain.getStadiumId());
        if (domain == null) {
            throw new YomsException("非法请求");
        } else {
            if (stadiumDao.deleteDomain(stadiumDomain) != 1) {
                throw new YomsException("删除场馆失败");
            }
        }
    }

    /**
     * 更新场馆
     *
     * @param stadiumDomain
     * @throws YomsException
     */
    public void upSatdium(StadiumDomain stadiumDomain) throws YomsException {
        StadiumDomain domain = stadiumDao.getDomainById(stadiumDomain.getStadiumId());
        if (domain == null) {
            throw new YomsException("非法请求");
        }
        if (stadiumDao.updateDomain(stadiumDomain) != 1) {
            throw new YomsException("更新公园场馆是失败");
        }
    }

    /**
     * 获取点击率最高带缩略图的场馆
     *
     * @return
     */
    public StadiumDomain getHottestStadium() {
        YomsPage yomsPage = new YomsPage();
        yomsPage.setPageSize(1);

        StadiumDomain stadiumDomain = new StadiumDomain();
        stadiumDomain.setStadiumImg("img");

        PageHelper.startPage(yomsPage.getPageIndex(), yomsPage.getPageSize(), true);
        PageHelper.orderBy("stadium_hot desc");
        List<StadiumDomain> newsDomains = this.stadiumDao.getDomainCollectionExtend(stadiumDomain, null, null);

        if (newsDomains.size() > 0) {
            return newsDomains.get(0);
        } else {
            return null;
        }
    }
}
